package com.wwj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwj.domain.LoginUser;
import com.wwj.domain.SysUser;
import com.wwj.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 继承UserDetailsService 实现重数据库查询用户信息。
 */
@Service
public class UserNameDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名在数据库中查询用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        SysUser sysUser = userMapper.selectOne(queryWrapper);
        //查询角色信息
        //TODO 先写死
        List<String> permissions = new ArrayList<>(Arrays.asList("admin","common"));

        //再封装成UserDetails实现类
        LoginUser loginUser = new LoginUser(sysUser,permissions);
        return loginUser;
    }
}
